package flow.service;

import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;

@Component
public class CountersignService {
    public boolean completeTask(DelegateExecution execution) {
        System.out.println("总任务数量：" + execution.getVariable("nrOfInstances"));
        System.out.println("未完成任务数量：" + execution.getVariable("nrOfActiveInstances"));
        System.out.println("已完成任务数量：" + execution.getVariable("nrOfCompletedInstances"));
        System.out.println("已批准任务数量：" + execution.getVariable("approveCount"));
        System.out.println("已拒绝任务数量：" + execution.getVariable("disapproveCount"));
        //自定义会签通过规则
        if ((Integer) execution.getVariable("nrOfCompletedInstances") >= (Integer) execution.getVariable("nrOfInstances")) {
            Integer approveCount = execution.getVariable("approveCount") == null ? 0 : (Integer) execution.getVariable("approveCount");
            Integer disapproveCount = execution.getVariable("disapproveCount") == null ? 0 : (Integer) execution.getVariable("disapproveCount");
            execution.setVariable("processResult", approveCount >= disapproveCount);
            //清空无用变量
            execution.setVariable("nrOfInstances", null);
            execution.setVariable("nrOfActiveInstances", null);
            execution.setVariable("nrOfCompletedInstances", null);
            execution.setVariable("approveCount", null);
            execution.setVariable("disapproveCount", null);
            return true;
        }
        return false;
    }
}
